package com.hairline.hairlinemarket.controller;

import com.github.pagehelper.PageInfo;
import com.hairline.hairlinemarket.bean.MarketGoods;
import com.hairline.hairlinemarket.bean.common.BaseRespVo;
import com.hairline.hairlinemarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Author:杨小凯
 * Date:2023-04-19 18:53
 * Description:<描述>
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/catalog")
    public BaseRespVo selectById(Long id){

        // 查询数据
        Map<String,Object> entity = goodsService.selectById(id);

        return BaseRespVo.ok(entity);
    }

    @RequestMapping("/detail")
    public BaseRespVo goodsDetail(Integer id){

        // 查询数据
        Map<String,Object> entity = goodsService.selectByGoodsId(id);

        return BaseRespVo.ok(entity);
    }

    /**
     * 查询相关信息
     * @param id
     * @return
     */
    @GetMapping("related")
    public BaseRespVo related(Integer id) {
        MarketGoods goods = goodsService.findById(id);
        if (goods == null) {
            return BaseRespVo.badArgument();
        }

        // 推荐同类目的其他商品
        int cid = goods.getCategoryId();

        // 查找六个相关商品
        int related = 6;
        //List<MarketGoods> goodsList = goodsService.queryByCategory(Arrays.asList(cid), 0, related);

        PageInfo<MarketGoods> goodsList = goodsService.queryByCategoryPage(cid, 0, related);

        return BaseRespVo.ok(goodsList);
    }

    @RequestMapping("/category")
    public BaseRespVo category(Integer id){

        Map<String,Object> map = goodsService.selectCategory(id);

        return BaseRespVo.ok(map);
    }

    @RequestMapping("list")
    public BaseRespVo list(Integer categoryId,Integer page,Integer limit){

        PageInfo<MarketGoods> pageInfo = goodsService.selectGoodsList(categoryId,page,limit);

        return BaseRespVo.ok(pageInfo);
    }


    @RequestMapping("/count")
    public BaseRespVo count(){
        Integer count = goodsService.selectGoodsCount();

        return BaseRespVo.ok(count);
    }
}
